
import Alamofire
import HandyJSON
import UIKit

class JXDependentExt: NSObject {

    open class JXBaseModel : NSObject ,HandyJSON {
        
        required override public init() {}
        
        open override var description:String{
            get{
                var dict = [String:AnyObject]()
                var count:UInt32 =  0
                let properties = class_copyPropertyList(type(of: self), &count)
                for i in 0..<count {
                    let t = property_getName((properties?[Int(i)])!)
                    if let n = NSString(cString: t, encoding: String.Encoding.utf8.rawValue) as String?
                    {
                        let v = self.value(forKey: n ) ?? "nil"
                        dict[n] = v as AnyObject?
                    }
                }
                return "\(type(of: self)):\(dict)\n"
            }
        }
        
        open override var debugDescription:String{
            get{
                return self.description
            }
        }
    }
    
    public class JX_NetworkManager {
        
        public enum RequestType {
            case GET
            case POST
        }
        
        public enum RequestStatus {
            case SUCCESS
            case FAILURE
            case NETWORK_ERROR
            case REQ_ERROR
        }
        
        // For Alamofire
        public class func requestData(showHUD:Bool? = false , type : RequestType , URLString:String , parameters:Parameters , dataCarrier:UIScrollView? = nil , completedClosure : @escaping ((_ resulet : Any)->()) , failureClosure: @escaping (_ resulet : Any)->()) {
            
            let requestType = type == .GET ? HTTPMethod.get : HTTPMethod.post
            
            print("{====Request_Url : \(URLString)====}")
            print("{====Request_Para : \(parameters)====}")
            
            if showHUD != nil {
                //show some HUD
            }
            
            
            Alamofire.request(URLString,method: requestType , parameters: parameters).responseJSON { (response) in
                
                /* do some custom action
                 SVProgressHUD.dismiss()
                 
                 if dataCarrier?.mj_header != nil {
                 dataCarrier?.mj_header.endRefreshing()
                 }
                 */
                
                //判断网络状况
                guard
                    let data = response.data,
                    let utf8DataStr = String(data: data, encoding: .utf8)
                    else{
                        
                        print("ErrorCode:\(response.result.error ?? "" as! Error)")
                        return
                }
                
                //do some error handle action or pass it to upper layer
                if let baseModel = JXBaseModel.deserialize(from: utf8DataStr){
                    /*
                     if baseModel.status == 1{
                     completedClosure(utf8DataStr)
                     }else{
                     SVProgressHUD.showError(withStatus: "请求错误\nErrorCode:\(baseModel.errorCodeMsg ?? "未定义错误") ")
                     failureClosure(utf8DataStr)
                     }
                     */
                }
            }
        }
    }

    
}
